﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common;
using ESOL.Business.Prod;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Proj
{
    public partial class frmMaintainProdPurImportItems : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Vars
        public List<ProdPlanOrderEntity> mSelectedProdPlanOrders { get; set; }
        #endregion

        #region Cons
        public frmMaintainProdPurImportItems()
        {
            InitializeComponent();
        }
        #endregion

        #region Func
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colUnit,"UNIT",true);
        }
        private void BindDataGrid()
        {
            if (mSelectedProdPlanOrders == null)
            {
                return;
            }
            DataTable dt = ProdPlanOrderBs.GetPlanToImportItemsForOrders(mSelectedProdPlanOrders);
            dt.Columns.Add("IS_SELECTED", typeof(Boolean));
            foreach (DataRow dr in dt.Rows)
            {
                dr["IS_SELECTED"] = false;
            }
            gvMain.DataSource = dt;
        }



        private bool Save()
        {
            DataTable dt = gvMain.DataSource as DataTable;
            DataTable dtTemp = dt.Clone();
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToBoolean(dr["IS_SELECTED"].ToString()) == true)
                {
                    DataRow[] aryDr = dtTemp.Select("PLAN_ORDER_ID='" + dr["PLAN_ORDER_ID"].ToString() + "'");
                    if (aryDr.Length == 0)
                    {
                        dtTemp.ImportRow(dr);
                    }
                    else
                    {
                        aryDr[0]["IMPORT_QTY"] = Convert.ToDecimal(aryDr[0]["IMPORT_QTY"]) + Convert.ToDecimal(dr["IMPORT_QTY"]);
                    }
                }
            }
            if (dtTemp.Rows.Count == 0)
            {
                MessageBox.Show("至少选择一条生产计划单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (Convert.ToDecimal(dr["QTY"]) > Convert.ToDecimal(dr["IMPORT_QTY"]))
                {
                    MessageBox.Show("生产计划单：" + dr["PLAN_ORDER_CODE"].ToString() + " 尚未采购完成！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            List<ProdPlanOrderEntity> mList=new List<ProdPlanOrderEntity>();
            foreach(DataRow dr in dtTemp.Rows )
            {
                ProdPlanOrderEntity entity=new ProdPlanOrderEntity(dr);
                mList.Add(entity);
            }
            string msg="";
            bool isSuccessful = ProdPlanOrderBs.ProcessPlanToImportItems(mList, msg);
            if (!isSuccessful)
            {
                MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            return true;
        }
        #endregion

        private void frmMaintainProdPurImportItems_Load(object sender, EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
            
        }
    }
}